using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Xml;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Serialization;

public partial class DatabaseManagement_TemplateExplorer : System.Web.UI.UserControl
{
    public TemplateStore templateStore;
    public delegate void NodeChange(object sender, OnNodeChangeEventArgs e);
    public event NodeChange OnNodeChange;
    private string imageFolder = "Images/folder.gif";
    private string imageScript = "Images/script.gif";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            templateStore = LoadTemplate(Server.MapPath(Request.ApplicationPath)+"\\Controls\\Database\\TemplateData.xml");
            foreach (TemplateDatabase database in templateStore.TemplateDatabase)
            {

                TreeNode dbNode = new TreeNode();
                dbNode.Text = database.subject;
                dbNode.ImageUrl = imageFolder;
                dbNode.NavigateUrl = "~/#";
                tvTemplateExplorer.Nodes.Add(dbNode);
                foreach (TemplateFolder folder in database.TemplateFunction)
                {
                    TreeNode folderNode = new TreeNode();
                    folderNode.Text = folder.subject;
                    folderNode.ImageUrl = imageFolder;
                    folderNode.NavigateUrl = "~/#";
                    dbNode.ChildNodes.Add(folderNode);
                    foreach (TemplateItem item in folder.TemplateItem)
                    {
                        TreeNode itemNode = new TreeNode();
                        itemNode.Text = item.key;
                        itemNode.Value = item.value;
                        itemNode.ToolTip = item.description;
                        itemNode.ImageUrl = imageScript;
                        folderNode.ChildNodes.Add(itemNode);
                    }
                    folderNode.Collapse();
                }

                foreach (TemplateFolder folder in database.TemplateScript)
                {
                    TreeNode folderNode = new TreeNode();
                    folderNode.Text = folder.subject;
                    folderNode.ImageUrl = imageFolder;
                    folderNode.NavigateUrl = "~/#";
                    dbNode.ChildNodes.Add(folderNode);
                    foreach (TemplateItem item in folder.TemplateItem)
                    {
                        TreeNode itemNode = new TreeNode();
                        itemNode.Text = item.key;
                        itemNode.Value = item.value;
                        itemNode.ToolTip = item.description;
                        itemNode.ImageUrl = imageScript;
                        folderNode.ChildNodes.Add(itemNode);
                    }
                    folderNode.Collapse();
                }
                dbNode.Expand();
            }
        }
    }

    public TemplateStore LoadTemplate(string fileName)
    {
        StreamReader stream = new StreamReader(fileName);
        string xml = stream.ReadToEnd();
        stream.Close();
        stream.Dispose();

        XmlSerializer serializer = new XmlSerializer(typeof(TemplateStore));
        StringReader reader = new StringReader(xml);
        try
        {
            templateStore = (TemplateStore)serializer.Deserialize(reader);
            return templateStore;
        }
        finally
        {
            reader.Close();
            reader.Dispose();
        }
    }


    //private string GetTemplate()
    //{
    //    TreeNode node = this.tvTemplateExplorer.SelectedNode;
    //    if (node == null || node.ImageUrl == this.imageFolder)
    //    {
    //        return string.Empty;
    //    }
    //    return node.Value + "\r\n\r\n" + node.ToolTip;
    //}

    protected void tvTemplateExplorer_SelectedNodeChanged(object sender, EventArgs e)
    {
        TreeNode node = this.tvTemplateExplorer.SelectedNode;
        if (node == null || node.ImageUrl == this.imageFolder)
        {
            return;
        }
        OnNodeChangeEventArgs eventArgs = new OnNodeChangeEventArgs();
        eventArgs.Value = node.Value;
        eventArgs.Tip = node.ToolTip;

        
        OnNodeChange(sender, eventArgs);
    }
}


public class OnNodeChangeEventArgs : EventArgs
{
    public string Value;
    public string Tip;
}
